import { Get, Model, Post } from '@xuekl/cli-base/annotate'
import R from '@xuekl/cli-base/r'
import { SelectItem, SideMenu } from '@xuekl/cli-base/type'
import { OperateResult } from './interface'
import http from '@/utils/httpRequest'

@Model('/xkl/local', false)
export default class LocalService {

    @Post('/insert')
    insert(res) {
        return new R<OperateResult>(res.code, res.data, res.msg).result()
    }

    @Post('/insertSub')
    insertSub(res) {
        return new R<OperateResult>(res.code, res.data, res.msg).result()
    }

    @Get('/menus')
    getMenus(res) {
        return new R<SideMenu[]>(res.code, res.data).result()
    }

    @Post('/rename')
    rename(res) {
        return new R<OperateResult>(res.code, res.data, res.msg).result()
    }

    @Get('/delete')
    delete(res) {
        return new R<OperateResult>(res.code, res.data, res.msg).result()
    }

    async getEntity(url: string) {
        const { data } = await http({
            url: http.adornUrl(url)
        })
        return data.data || data.rows || data.page
    }

    @Post('/genEntity')
    genEntity(res) {
        return new R<OperateResult>(res.code, res.data, res.msg).result()
    }

    @Get('/getWidgets')
    getWidgets(res) {
        return new R<SelectItem[]>(res.code, res.data, res.msg).result()
    }


    sendToken() {
        let proxy = {}
        // @ts-ignore
        import("../../../proxy").then(res => {
            proxy = res.default
            http({
                url: '/xkl/local/sendToken',
                params: http.adornParams({
                    url: proxy['/api'].target,
                    tokenKey: 'authorization'
                })
            })
        })
    }
}